package com.nitty.cash.loop.ui.activity

import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.lib.common.adapter.CommonOnItemChildClickListener
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonActivity
import com.nitty.cash.loop.databinding.UiOrderListBinding
import com.nitty.cash.loop.event.NittyEventData
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.ui.adapter.OrderListAdapter
import com.nitty.cash.loop.utils.NittyTitleBarUtil
import com.nitty.cash.loop.utils.helper.NittyOrderHelper
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/12/18 17:51
 * @Annotation：订单列表
 */
class OrderListAct: CommonActivity(){
    lateinit var binding: UiOrderListBinding
    private var orderAdapter: OrderListAdapter? = null
    override fun getNittyRootView(): View? {
        binding = UiOrderListBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initNittyView() {
        NittyTitleBarUtil.setStatusBarLightMode(this, false)
        setNittyTitleBarVisibility(View.VISIBLE)
        setNittyTextTitleVisibility(View.GONE)
        setNittyBackBtnRes(R.mipmap.img_back_white)
        setNittyRightBtnRes(R.mipmap.img_customer_white)
        setNittyRootParentBackgroundColors(Color.parseColor("#ffffd451"), Color.parseColor("#ffffac17"))
        setNittyRootBackgroundLableRes(R.mipmap.nitty_withdraw_titlebar_bg)
        initOrderListRefrashView()
        initOrderListView()
    }

    override fun initNittyListener() {
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("OrderList Page")
            addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_CUSTOMER,"OrderList Page")
        }
    }

    override fun loadNittyData() {
        val orderList = NittyOrderHelper.getNittyOrderOtherList()
        if (orderList.size == 0){
            orderAdapter?.clear()
            val emptyItem = JSONObject()
            emptyItem.put("isEmptyView",true)
            orderAdapter?.addFirstItem(emptyItem)
            return
        }
        orderAdapter?.data = orderList
    }

    /* 初始化刷新 */
    private fun initOrderListRefrashView() {
        binding.orderListRefreshView.setColorSchemeResources(R.color.theme,R.color.purple_500)
        binding.orderListRefreshView.setOnRefreshListener {
            publishEventData(NittyEventData(NittyEventData.TYPE.NITTY_ORDER_REFRESH))
        }
    }

    private fun initOrderListView() {
        binding.rvOrderList.layoutManager = LinearLayoutManager(this)
        orderAdapter = OrderListAdapter(binding.rvOrderList)
        binding.rvOrderList.adapter = orderAdapter
        binding.rvOrderList.itemAnimator = DefaultItemAnimator()
        orderAdapter?.setOnItemChildClickListener(CommonOnItemChildClickListener { _, childView, position ->
            val item = orderAdapter!!.getItem(position)
            when(childView.id){
                R.id.textWithdraw -> {//去提现
//                    ApplyAct.start(this)
                    publishEventData(NittyEventData(NittyEventData.TYPE.NITTY_SHOW_HOME))
                    finish()
                }
                R.id.btnOrderDetails -> {//查看详情
                    OrderDetailsAct.start(this,item.optString("orderId"))
                }
            }
        })
    }

    override fun onNittyPublishEvent(event: NittyEventData) {
        super.onNittyPublishEvent(event)
        when(event.eventType){
            NittyEventData.TYPE.NITTY_ORDER_REFRESHED -> {
                loadNittyData()
            }
            NittyEventData.TYPE.NITTY_ORDER_FINISHED -> {
                hiddenNittyLoading()
            }
            else -> {

            }
        }
    }

    override fun hiddenNittyLoading() {
        super.hiddenNittyLoading()
        binding.orderListRefreshView.isRefreshing = false
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_ORDER_LIST_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(NittyEventPointType.PAGE.NITTY_PAGE_ORDER_LIST_END)
    }
}